#include "ServerPlayer.h"
#include "Room.h"

#include <QTcpSocket>

ServerPlayer::ServerPlayer(Room *room, int id, QTcpSocket *socket){
    setObjectName("ServerPlayer");

    setParent(room);
    this->room = room;
    this->player_id = id;
    this->socket = socket;

    connect(socket, SIGNAL(readyRead()), this, SLOT(receiveNotification()));
    notifyProperty("player_id");
}

Room *ServerPlayer::getRoom() const{
    return room;
}

void ServerPlayer::chat(QString message){
    room->sendChatMessage(this, message);
}

void ServerPlayer::movePiece(int from, int to){
    room->sendMovePieceAction(this->getPlayerId(), from, to);
}

QHostAddress ServerPlayer::getClientAddress() const{
    return socket->peerAddress();
}

void ServerPlayer::receiveNotification(){
    qDebug() << "ServerPlayer: receiving...";
    while(socket->canReadLine()){        
        QByteArray line = socket->readLine();
        line.remove(line.length() - 1, 1);

        //qDebug() << "ServerPlayer:" << line;

        int mid = line.indexOf('=');
        if(mid <= 0){
            qDebug() << "ServerPlayer: Illegal Message";
            return;
        }

        CommandType command = CommandType(line.left(mid).at(0) - '0');
        QByteArray data = QByteArray::fromBase64(line.mid(mid + 1));

        switch(command){
        case Chat:{
            chat(QString::fromUtf8(data));
            break;
        }
        case MovePiece:{
            QList<QByteArray> p = data.split(' ');
            if(p.length() != 2){
                qDebug() << "ServerPlayer: MovePiece Illegal Data";
                return;
            }
            int from = p.at(0).toInt();
            int to = p.at(1).toInt();
            qDebug() << "ServerPlayer:" << to;
            movePiece(from, to);
            break;
        }
        case NewPlayer:{
            setPlayerName(QString::fromUtf8(data));
            room->broadcast(NewPlayer, toString());
            break;
        }
        case SetProperty:{
            int i = data.indexOf(' ');
            QByteArray p_key = data.left(i);
            QString p_value = QString::fromUtf8(data.mid(i + 1));
            setProperty(p_key.data(), p_value);
            room->sendPlayerProperty(this, QString::fromUtf8(p_key), p_value);

            if(p_key == "is_ready" && this->isReady()){
                room->addReadyPlayer();//TODO Bug Duplicated ready message
            }else if(p_key == "phase"){
                if(this->getPhase() == End){
                    room->activateNextPlayer();
                }
            }

            break;
        }
        case SetPlayer:{
            QList<QByteArray> p = data.split(' ');
            if(p.length() != 3){
                qDebug() << "ServerPlayer: SetPlayer Illegal Data";
                return;
            }
            int player_id = p.at(0).toInt();
            QByteArray p_key = p.at(1);
            QVariant p_value = p.at(2);
            if(player_id == this->player_id){
                setProperty(p_key.data(), p_value);
            }
            room->broadcast(SetPlayer, data);
            break;
        }
        case GameOver:{
            room->endGame();
            break;
        }
        case NoAction:default:;
        }
    }
}
